package com.finger.crowd.service.api.impl;

import com.finger.crowd.constant.CrowdConstant;
import com.finger.crowd.entity.Admin;
import com.finger.crowd.entity.AdminExample;
import com.finger.crowd.exception.LoginFailedException;
import com.finger.crowd.mapper.AdminMapper;
import com.finger.crowd.service.api.AdminService;
import com.finger.crowd.util.CrowdUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    public void saveAdmin(Admin admin) {

        String userPswd = admin.getUserPswd();

        String encodePswd = passwordEncoder.encode(userPswd);

        admin.setUserPswd(encodePswd);

        Date date = new Date();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        String format = simpleDateFormat.format(date);

        admin.setCreateTime(format);

        adminMapper.insert(admin);
    }

    public List<Admin> getAllAdmin() {
        return adminMapper.selectByExample(new AdminExample());
    }

    public Admin queryAdminById(int id) {
        return adminMapper.selectByPrimaryKey(id);
    }

    public Admin getAdminByLoginAccount(String loginAcct, String userPswd) {
        if (loginAcct.length() == 0 || loginAcct == null ||

                userPswd.length() == 0 || userPswd == null) {

            throw new LoginFailedException("请不要输入空字符串");
        }
        // 创建一个例子
//        AdminExample adminExample = new AdminExample();
//
//        AdminExample.Criteria criteria = adminExample.createCriteria();
//
//        criteria.andLoginAcctEqualTo(loginAcct);

        Admin admin = null;
        try {
            admin = adminMapper.selectAdminByLoginAcct(loginAcct);
        } catch (Exception exception) {
            exception.printStackTrace();
            throw new RuntimeException(CrowdConstant.MESSAGE_SYSTEM_ERROR_LOGIN_NOT_UNIQUE);
        }

//        // 根据账号查询用户是否存在
//        List<Admin> admins = adminMapper.selectByExample(adminExample);

        // 判断数据库中是否有此对象
//        if (admins.size() == 0 || admins == null) {
//
//            throw new LoginFailedException("账号错误，请重新输入");
//
//        }

//        if (admins.size() > 1) {
//
//            throw new RuntimeException(CrowdConstant.MESSAGE_SYSTEM_ERROR_LOGIN_NOT_UNIQUE);
//
//        }
//        if (admin == null) {
//            throw new LoginFailedException("账号错误，请重新输入");
//        }


//        Admin admin = admins.get(0);

        if (admin == null) {
            throw new LoginFailedException(CrowdConstant.MESSAGE_LOGIN_FAILED);
        }

        String userPswd1 = admin.getUserPswd();

        String userPswdEnd = CrowdUtils.md5(userPswd);

        if (!Objects.equals(userPswd1, userPswdEnd)) {

            throw new LoginFailedException("密码输入错误,请重新输入");

        }

        return admin;
    }

    @Override
    public PageInfo<Admin> getPageInfo(String keyword, Integer pageNum, Integer pageSize) {

        // 开启分页
        PageHelper.startPage(pageNum, pageSize);

        // 根据keyword查询用户
        List<Admin> admins = adminMapper.selectAdminByKeyword(keyword);

        return new PageInfo<>(admins);
    }

    @Override
    public void removeAdmin(Integer id) {
        adminMapper.deleteByPrimaryKey(id);
    }

    @Override
    public boolean queryAdminByLoginAcct(String loginAcct) {

        Admin admin = adminMapper.selectAdminByLoginAcct(loginAcct);

        if (admin == null) {

            return false;

        }
        return true;
    }

    @Override
    public void updateAdmin(Admin admin) {
        try {

            adminMapper.updateByPrimaryKeySelective(admin);

        } catch (Exception exception) {
            exception.printStackTrace();
            throw new DuplicateKeyException("用户名已被占用");
        }
    }

    @Override
    public void deleteOldRelationship(Integer adminId) {
        adminMapper.deleteOldRelationShip(adminId);
    }

    @Override
    public void insertNewRelationship(Integer adminId, List<Integer> roleIdList) {

        adminMapper.insertNewRelationShip(adminId, roleIdList);

    }


    @Override
    public Admin getAdminByLoginAcct(String loginAcct) {

        AdminExample adminExample = new AdminExample();

        AdminExample.Criteria criteria = adminExample.createCriteria();

        criteria.andLoginAcctEqualTo(loginAcct);

        List<Admin> admins = adminMapper.selectByExample(adminExample);

        Admin admin = admins.get(0);

//        Admin admin = adminMapper.selectAdminByLoginAcct(loginAcct);

        return admin;
    }
}
